package com.huhu.scenes;

import java.util.ArrayList;

import org.andengine.engine.camera.hud.HUD;
import org.andengine.engine.handler.timer.ITimerCallback;
import org.andengine.engine.handler.timer.TimerHandler;
import org.andengine.entity.primitive.Rectangle;
import org.andengine.entity.scene.background.Background;
import org.andengine.entity.sprite.Sprite;
import org.andengine.extension.physics.box2d.FixedStepPhysicsWorld;
import org.andengine.extension.physics.box2d.PhysicsFactory;
import org.andengine.extension.physics.box2d.PhysicsWorld;
import org.andengine.extension.tmx.TMXLayer;
import org.andengine.extension.tmx.TMXTiledMap;
import org.andengine.input.touch.TouchEvent;
import org.andengine.util.color.Color;

import android.util.Log;

import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.physics.box2d.Body;
import com.badlogic.gdx.physics.box2d.BodyDef.BodyType;
import com.badlogic.gdx.physics.box2d.FixtureDef;
import com.huhu.GameActivity;
import com.huhu.base.BaseScene;
import com.huhu.extras.GameOverWindow;
import com.huhu.extras.LevelCompleteWindow;
import com.huhu.interfaces.IPlayer;
import com.huhu.levelloader.LevelLoader1_1;
import com.huhu.levelloader.LevelLoader1_2;
import com.huhu.levelloader.LevelLoader1_3;
import com.huhu.levelloader.LevelLoader1_4;
import com.huhu.levelloader.LevelLoader1_5;
import com.huhu.manager.ResourcesManager;
import com.huhu.manager.SceneManager;
import com.huhu.manager.SceneManager.SceneType;
import com.huhu.objects.Player;
import com.huhu.objects.TextSprite;

public class GameScene extends BaseScene {

	private HUD gameHUD;
	private PhysicsWorld physicsWorld;

	private IPlayer player;

	private boolean gameOverDisplayed = false;
	private GameOverWindow gameOverWindow;
	private boolean levelCompleteDisplayed = false;
	private LevelCompleteWindow levelCompleteWindow;

	private TMXTiledMap mTMXTiledMap;

	public GameScene() {
		super();
	}

	@Override
	public void createScene() {
		// TODO Auto-generated method stub
		createBackground();
//		createHUD();
		createPhysics();
		loadLevel();
		gameOverWindow = new GameOverWindow(vbom);
		levelCompleteWindow = new LevelCompleteWindow(vbom);
	}

	@Override
	public void onBackKeyPressed() {
		SceneManager.getInstance().loadLevelScene(engine);

	}

	@Override
	public SceneType getSceneType() {
		// TODO Auto-generated method stub
		return SceneType.GAME;
	}

	@Override
	public void disposeScene() {
		camera.setHUD(null);
		camera.setCenter(GameActivity.CAMERA_WIDTH/2, GameActivity.CAMERA_HEIGHT/2);
		camera.setChaseEntity(null);
	}

	private void createBackground() {
		setBackground(new Background(Color.BLUE));
	}

	private void createHUD() {
		gameHUD = new HUD();

		final Sprite spriteMoveLeft = new Sprite(10, 520,
				resourcesManager.move_left_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					player.setDirection(false);
					player.setRunning();
				} else {
					player.setStopping();
				}
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteMoveLeft);
		gameHUD.attachChild(spriteMoveLeft);

		final Sprite spriteMoveRight = new Sprite(135, 520,
				resourcesManager.move_right_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				if (pSceneTouchEvent.isActionDown()) {
					player.setDirection(true);
					player.setRunning();
				} else {
					player.setStopping();
				}
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteMoveRight);
		gameHUD.attachChild(spriteMoveRight);

		final Sprite spriteJump = new Sprite(876, 520,
				resourcesManager.jump_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				player.jump();

				return true;
			}
		};
		gameHUD.registerTouchArea(spriteJump);
		gameHUD.attachChild(spriteJump);

		final Sprite spriteBack = new Sprite(10, 10,
				resourcesManager.back_region, vbom) {
			@Override
			public boolean onAreaTouched(TouchEvent pSceneTouchEvent,
					float pTouchAreaLocalX, float pTouchAreaLocalY) {
				SceneManager.getInstance().getCurrentScene().onBackKeyPressed();
				return true;
			}
		};
		gameHUD.registerTouchArea(spriteBack);
		gameHUD.attachChild(spriteBack);

		camera.setHUD(gameHUD);
	}

	private void createPhysics() {
		physicsWorld = new FixedStepPhysicsWorld(60, new Vector2(0, 50), true, 8, 1);
		// physicsWorld.setContactListener(contactListener());
		registerUpdateHandler(physicsWorld);
	}

	// load map from asset
	private void loadLevel() {
		mTMXTiledMap = ResourcesManager.getInstance().loadMap(
				SceneManager.currentLevel);
		final ArrayList<TMXLayer> mTMXLayers = mTMXTiledMap.getTMXLayers();
		final TMXLayer mTMXLayer = mTMXLayers.get(0);
		camera.setBounds(0, 0, mTMXLayer.getWidth(), mTMXLayer.getHeight());
		camera.setBoundsEnabled(true);

		for (TMXLayer layer : mTMXLayers) {
			if (layer.getTMXLayerProperties().containsTMXProperty("background",
					"true")
					|| layer.getTMXLayerProperties().containsTMXProperty(
							"platform", "true")
					|| layer.getTMXLayerProperties().containsTMXProperty(
							"decoration", "true")) {
				
				this.attachChild(layer);
			}
		}

		final FixtureDef FIXTURE_DEF = PhysicsFactory.createFixtureDef(0, 0, 0);
		final Rectangle leftRec = new Rectangle(0, 0, 1, mTMXLayer.getHeight(),
				vbom);
		final Rectangle rightRec = new Rectangle(mTMXLayer.getWidth() - 1, 0,
				1, mTMXLayer.getHeight(), vbom);
		PhysicsFactory.createBoxBody(physicsWorld, leftRec,
				BodyType.StaticBody, FIXTURE_DEF);
		PhysicsFactory.createBoxBody(physicsWorld, rightRec,
				BodyType.StaticBody, FIXTURE_DEF);
		leftRec.setVisible(false);
		rightRec.setVisible(false);
		this.attachChild(leftRec);
		this.attachChild(rightRec);

		// createPlatform();
		switch (SceneManager.currentLevel) {
		case 1:
			LevelLoader1_1 levelLoader1_1 = new LevelLoader1_1(engine, this,
					camera, physicsWorld, vbom);
			player = levelLoader1_1.createPlatform(mTMXTiledMap);
			break;
		case 2:
			LevelLoader1_2 levelLoader1_2 = new LevelLoader1_2(engine, this,
					camera, physicsWorld, vbom);
			player = levelLoader1_2.createPlatform(mTMXTiledMap);
			break;
		case 3:
			LevelLoader1_3 levelLoader1_3 = new LevelLoader1_3(engine, this, camera, physicsWorld, vbom);
			player = levelLoader1_3.createPlatform(mTMXTiledMap);
			break;
		case 4:
			LevelLoader1_4 levelLoader1_4 = new LevelLoader1_4(engine, this, camera, physicsWorld, vbom);
			player = levelLoader1_4.createPlatform(mTMXTiledMap);
			break;
		case 5:
			LevelLoader1_5 levelLoader1_5 = new LevelLoader1_5(engine, this, camera, physicsWorld, vbom);
			player = levelLoader1_5.createPlatform(mTMXTiledMap);
			break;
		case 6:

			break;
		}

	}

	public void displayLevelComplete(int level) {
		levelCompleteWindow.display(level, this, camera);
	}

	public void displayGamOver() {
		gameOverDisplayed = true;
		engine.registerUpdateHandler(new TimerHandler(1, new ITimerCallback() {

			@Override
			public void onTimePassed(TimerHandler pTimerHandler) {
				engine.unregisterUpdateHandler(pTimerHandler);
				gameOverWindow.display(0, GameScene.this, camera);
			}
		}));

	}
}
